##########################################
######  Title: Pakistan Figures     ######
######  Date: November 7, 2015      ######
##########################################

# Load Packages

library(foreign)
library(ggplot2)
library(gridExtra)
library(reshape2)
library(scales)

# Set Working Directory

setwd("~/Dropbox/PK2 share/Replication/Data")

# Load Data

pkdata <- read.dta("FLMS_2015_prepped.dta")
pk <- read.csv("pkdata.csv")
ap <- read.table("pk2_airplane.csv", sep=",", header=TRUE)

###################################################
################## PAPER FIGURES ##################
###################################################

### OVERVIEW OF PAPER FIGURES ###  

# Figure 1: Endorsement Effects and Diagnostic Checks
# Figure 2: Observational Poverty Results and Results of the Relative Poverty Experiment
# Figure 3: Results of the Perceived Violence Experiment
# Figure 4: Interaction of Relative Poverty and Perceived Violence Experiments
# Figure 5: Treatment Effects of Relative Poverty and Perceived Violence by Observed Poverty


############################################
## FIGURE 1: OVERALL ENDORSEMENT EFFECTS ##
############################################

## ENDORSEMENT EFFECTS

pk$overall.ci.lower <- pk$overall.coef - (qnorm(.975) * pk$overall.se)
pk$overall.ci.upper <- pk$overall.coef + (qnorm(.975) * pk$overall.se)

## EDHI VALIDITY CHECK

pk$support.ci.lower <- pk$support.coef - (qnorm(.95) * pk$support.se)
pk$support.ci.upper <- pk$support.coef + (qnorm(.95) * pk$support.se)

pk$nosupport.ci.lower <- pk$nosupport.coef - (qnorm(.95) * pk$nosupport.se)
pk$nosupport.ci.upper <- pk$nosupport.coef + (qnorm(.95) * pk$nosupport.se)

## HETEROGENEITY BY OVERALL POLITICAL KNOWLEDGE (ADDITIVE INDEX)

pk$gknow.low.ci.lower <- pk$gknow.low.coef - (qnorm(.975) * pk$gknow.low.se)
pk$gknow.low.ci.upper <- pk$gknow.low.coef + (qnorm(.975) * pk$gknow.low.se)

pk$gknow.high.ci.lower <- pk$gknow.high.coef - (qnorm(.975) * pk$gknow.high.se)
pk$gknow.high.ci.upper <- pk$gknow.high.coef + (qnorm(.975) * pk$gknow.high.se)

## CREATE FIGURE 1

figure1.1 <- ggplot(data = pk[-5,], aes(x = group, y = overall.coef)) +
  coord_cartesian(ylim = c(-.065, .0675)) +
  geom_point(size = 2.5) +
  geom_hline(aes(yintercept=0), colour="red", linetype="dashed") +
  geom_point(aes(x = group, y = gknow.low.coef), shape=0) +
  geom_point(aes(x = group, y = gknow.high.coef), shape=1) +
  geom_hline(aes(yintercept=0), colour="red", linetype="dashed") +
  ylab("Endorsement Effect") +
  ggtitle("(a) Support for Militant Groups") +
  theme(axis.title.x = element_blank(), axis.title.y = element_text(size=10),
        axis.text.x=element_text(angle=34.5, hjust=1, size = 8),
        axis.text.y=element_text(size = 8),
        plot.title = element_text(size = 10),
        panel.grid.major.y = element_line(size=.3),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank()) +
  scale_x_discrete(limits=c("militant.groups", "ssp", "pak.tal", "afghan.tal"),
                   labels = c("Militant Group \n Average", "Sipah-e-Sahaba \n Pakistan",
                     "Pakistan Taliban", "Afghan Taliban")) +
  geom_errorbar(aes(ymin=overall.ci.lower, ymax=overall.ci.upper, width=.2))

figure1.2 <- ggplot(data = pk[5,], aes(x = c("Overall Support", "Explicit Support",
                                         "No Explicit Support"),
                      y = c(overall.coef, support.coef, nosupport.coef))) +
  coord_cartesian(ylim = c(-.065, .0675)) +
  geom_point(size = 2.5) +
  geom_hline(aes(yintercept=0), colour="red", linetype="dashed") +
  ylab("Endorsement Effect") +
  ggtitle("(b) Support for Edhi") +
  theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
        axis.text.x=element_text(angle=35, hjust=1, size = 8),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        plot.title = element_text(size = 10),
        panel.grid.major.y = element_line(size=.3),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank()) +
  scale_x_discrete(limits=c("Overall Support", "Explicit Support",
                     "No Explicit Support"), labels=c("Full Sample", "Explicit \n Supporters","Explicit \n Non-Supporters")) +
  geom_errorbar(aes(ymin=c(overall.ci.lower, support.ci.lower, nosupport.ci.lower),
                    ymax=c(overall.ci.upper, support.ci.upper, nosupport.ci.upper),
                    width=.2))

pdf(file = "~/Dropbox/PK2 share/Replication/Figures/figure1.pdf", height = 4, width = 7)
figure1 <- grid.arrange(figure1.1, figure1.2, ncol=2, widths = c(.6, .4))
dev.off()


################################################
## FIGURE 2: POVERTY AND ENDORSEMENT EFFECTS ##
################################################

## RELATIONSHIP BETWEEN SES AND ENDORSEMENT

pk$upper.ci.lower <- pk$upper.coef - (qnorm(.95) * pk$upper.se)
pk$upper.ci.upper <- pk$upper.coef + (qnorm(.95) * pk$upper.se)

pk$middle.ci.lower <- pk$middle.coef - (qnorm(.95) * pk$middle.se)
pk$middle.ci.upper <- pk$middle.coef + (qnorm(.95) * pk$middle.se)

pk$lower.ci.lower <- pk$lower.coef - (qnorm(.95) * pk$lower.se)
pk$lower.ci.upper <- pk$lower.coef + (qnorm(.95) * pk$lower.se)

pk$ul.diff.ci.lower <- pk$ul.diff.coef - (qnorm(.95) * pk$ul.diff.se)
pk$ul.diff.ci.upper <- pk$ul.diff.coef + (qnorm(.95) * pk$ul.diff.se)

## POVERTY SURVEY EXPERIMENT

pk$lowpov.ci.lower <- pk$lowpov.coef - (qnorm(.95) * pk$lowpov.se)
pk$lowpov.ci.upper <- pk$lowpov.coef + (qnorm(.95) * pk$lowpov.se)

pk$highpov.ci.lower <- pk$highpov.coef - (qnorm(.95) * pk$highpov.se)
pk$highpov.ci.upper <- pk$highpov.coef + (qnorm(.95) * pk$highpov.se)

pk$pov.diff.ci.lower <- pk$pov.diff.coef - (qnorm(.95) * pk$pov.diff.se)
pk$pov.diff.ci.upper <- pk$pov.diff.coef + (qnorm(.95) * pk$pov.diff.se)

## CREATE FIGURE 2

figure2 <- ggplot(data = pk[1,], aes(x = c("Upper Class", "Middle Class",
                                       "Lower Class", "Lower - Upper \n Class Difference", "Relatively Wealthy",
                                       "Relatively Poor", "Poor - Wealthy \n Difference"),
                    y = c(upper.coef, middle.coef, lower.coef, ul.diff.coef, lowpov.coef,
                      highpov.coef, pov.diff.coef))) +
  coord_cartesian(ylim = c(-.125, .09)) +
  geom_point(size = 2.5,
             color = c("black", "black", "black", "#666666", "black", "black", "#666666")) +
  geom_hline(aes(yintercept=0), colour="red", linetype="dashed") +
  geom_vline(aes(xintercept=4.5), size=.5, color="black") +
  ylab("Endorsement Effect") +
  # ggtitle("Poverty and Support for Militancy") +
  theme(axis.title.x = element_blank(), axis.title.y = element_text(size=10),
        axis.text.x=element_text(angle=35, hjust=1, size = 8),
        axis.text.y=element_text(size = 8),
        plot.title = element_text(size = 12),
        panel.grid.major.y = element_line(size=.3),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank()) +
  scale_x_discrete(limits=c("Upper Class", "Middle Class", "Lower Class", "Lower - Upper \n Class Difference",
                     "Relatively Wealthy", "Relatively Poor", "Poor - Wealthy \n Difference"), labels=c("Upper Class \n Respondents","Middle Class \n Respondents","Lower Class \n Respondents", "Lower - Upper \n Class Difference", "Relatively Wealthy \n Condition","Relatively Poor \n Condition","Poor - Wealthy \n Difference")) +
  geom_errorbar(aes(ymin=c(upper.ci.lower, middle.ci.lower, lower.ci.lower, ul.diff.ci.lower, 
                           lowpov.ci.lower, highpov.ci.lower, pov.diff.ci.lower),
                    ymax=c(upper.ci.upper, middle.ci.upper, lower.ci.upper, ul.diff.ci.upper,
                      lowpov.ci.upper, highpov.ci.upper, pov.diff.ci.upper),
                    width=.2),
                color = c("black", "black", "black", "#666666", "black", "black",
                  "#666666")) +
annotate("text", label = "(a) Self-reported SES", x = 1.5, y = .075, size = 3.5) +
annotate("text", label = "(b) Poverty Experiment", x = 5.625, y = .075, size = 3.5)
ggsave(filename = "~/Dropbox/PK2 share/Replication/Figures/figure2.pdf", width = 6, height = 4)


#################################################
## FIGURE 3: VIOLENCE AND ENDORSEMENT EFFECTS ##
#################################################

## NATIONAL VIOLENCE EXPERIMENT

pk$lowviol.ci.lower <- pk$lowviol.coef - (qnorm(.95) * pk$lowviol.se)
pk$lowviol.ci.upper <- pk$lowviol.coef + (qnorm(.95) * pk$lowviol.se)

pk$highviol.ci.lower <- pk$highviol.coef - (qnorm(.95) * pk$highviol.se)
pk$highviol.ci.upper <- pk$highviol.coef + (qnorm(.95) * pk$highviol.se)

pk$viol.diff.ci.lower <- pk$viol.diff.coef - (qnorm(.95) * pk$viol.diff.se)
pk$viol.diff.ci.upper <- pk$viol.diff.coef + (qnorm(.95) * pk$viol.diff.se)

## CREATE FIGURE 3

figure3 <- ggplot(data = pk[1,], aes(x = c("Less Violence", "More Violence",
                                       "More - Less \n Difference"),
                    y = c(lowviol.coef, highviol.coef, viol.diff.coef))) +
  coord_cartesian(ylim = c(-.225, .125)) +
  geom_point(size = 2.5, color = c("black", "black", "#666666")) +
  geom_hline(aes(yintercept=0), colour="red", linetype="dashed") +
  ylab("Endorsement Effect") +
  # ggtitle("Violence Experiment and Support for Militancy") +
  theme(axis.title.x = element_blank(), axis.title.y = element_text(size=10),
        axis.text.x=element_text(angle=35, hjust=1, size = 8),
        axis.text.y=element_text(size = 8),
        plot.title = element_text(size = 12),
        panel.grid.major.y = element_line(size=.3),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank()) +
  scale_x_discrete(limits=c("Less Violence", "More Violence",
                     "More - Less \n Difference"), labels=c("Less Violence \n Condition","More Violence \n Condition","More - Less \n Difference")) +
  geom_errorbar(aes(ymin=c(lowviol.ci.lower, highviol.ci.lower,
                      viol.diff.ci.lower),
                    ymax=c(lowviol.ci.upper, highviol.ci.upper,
                      viol.diff.ci.upper), width=.2),
                color = c("black", "black", "#666666"))
ggsave(filename = "~/Dropbox/PK2 share/Replication/Figures/figure3.pdf", width = 5, height = 4)


###########################################################
## FIGURE 4: POVERTY, VIOLENCE, AND ENDORSEMENT EFFECTS ##
###########################################################

## POVERTY, VIOLENCE, AND ENDORSEMENT INTERACTION (2-WAY)

pk$hphv.ci.lower <- pk$hphv.coef - (qnorm(.95) * pk$hphv.se)
pk$hphv.ci.upper <- pk$hphv.coef + (qnorm(.95) * pk$hphv.se)

pk$lphv.ci.lower <- pk$lphv.coef - (qnorm(.95) * pk$lphv.se)
pk$lphv.ci.upper <- pk$lphv.coef + (qnorm(.95) * pk$lphv.se)

pk$hplv.ci.lower <- pk$hplv.coef - (qnorm(.95) * pk$hplv.se)
pk$hplv.ci.upper <- pk$hplv.coef + (qnorm(.95) * pk$hplv.se)

pk$lplv.ci.lower <- pk$lplv.coef - (qnorm(.95) * pk$lplv.se)
pk$lplv.ci.upper <- pk$lplv.coef + (qnorm(.95) * pk$lplv.se)

pk$hphv.lplv.ci.lower <- pk$hphv.lplv.coef - (qnorm(.95) * pk$hphv.lplv.se)
pk$hphv.lplv.ci.upper <- pk$hphv.lplv.coef + (qnorm(.95) * pk$hphv.lplv.se)

pk$hphv.hplv.ci.lower <- pk$hphv.hplv.coef - (qnorm(.95) * pk$hphv.hplv.se)
pk$hphv.hplv.ci.upper <- pk$hphv.hplv.coef + (qnorm(.95) * pk$hphv.hplv.se)

pk$hphv.lphv.ci.lower <- pk$hphv.lphv.coef - (qnorm(.95) * pk$hphv.lphv.se)
pk$hphv.lphv.ci.upper <- pk$hphv.lphv.coef + (qnorm(.95) * pk$hphv.lphv.se)

## CREATE FIGURE 4

figure4 <- ggplot(data = pk[1,], aes(x = c("High Poverty \n High Violence",
                                       "Low Poverty \n High Violence",
                                       "High Poverty \n Low Violence",
                                       "Low Poverty \n Low Violence",
                                       "HPHV - LPLV", "HPHV - HPLV",
                                       "HPHV - LPHV"),
                    y = c(hphv.coef, lphv.coef, hplv.coef, lplv.coef,
                      hphv.lplv.coef, hphv.hplv.coef, hphv.lphv.coef))) +
  coord_cartesian(ylim = c(-.275, .175)) +
  geom_point(size = 2.5,
             color = c("black", "black", "black", "black", "#666666", "#666666",
               "#666666")) +
  geom_hline(aes(yintercept=0), colour="red", linetype="dashed") +
  geom_vline(aes(xintercept=4.5), size=.5) +
  ylab("Endorsement Effect") +
  # ggtitle("Poverty, Violence, and Support for Militancy") +
  theme(axis.title.x = element_blank(), axis.title.y = element_text(size=10),
        axis.text.x=element_text(angle=35, hjust=1, size = 7.5),
        axis.text.y=element_text(size = 8),
        plot.title = element_blank(),
        panel.grid.major.y = element_line(size=.3),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank()) +
  scale_x_discrete(limits= c("High Poverty \n High Violence",
                     "Low Poverty \n High Violence",
                     "High Poverty \n Low Violence", "Low Poverty \n Low Violence",
                     "HPHV - LPLV", "HPHV - HPLV", "HPHV - LPHV"),
                   labels= c("Relatively Poor \n More Violence",
                     "Relatively Wealthy \n More Violence",
                     "Relatively Poor \n Less Violence",
                     "Relatively Wealthy \n Less Violence", "Relatively Wealthy \n Less Violence",
                     "Relatively Poor \n Less Violence",
                     "Relatively Wealthy \n More Violence")) +
  geom_errorbar(aes(ymin=c(hphv.ci.lower, lphv.ci.lower, hplv.ci.lower,
                      lplv.ci.lower, hphv.lplv.ci.lower, hphv.hplv.ci.lower,
                      hphv.lphv.ci.lower),
                    ymax=c(hphv.ci.upper, lphv.ci.upper, hplv.ci.upper,
                      lplv.ci.upper, hphv.lplv.ci.upper, hphv.hplv.ci.upper,
                      hphv.lphv.ci.upper), width=.2),
                color = c("black", "black", "black", "black", "#666666", "#666666",
                  "#666666")) +
annotate("text", label = "(a) Interaction between \n Poverty and Violence Conditions", x = 2.4, y = .14,
         size = 3) +
annotate("text", label = "(b) How much lower is support in \n Relatively Poor / More Violence than in...", x = 6.05,
         y = .14, size = 3)
ggsave(filename = "~/Dropbox/PK2 share/Replication/Figures/figure4.pdf", width = 6.5, height = 4)


################################################################
## FIGURE 5: SES, POVERTY, VIOLENCE, AND ENDORSEMENT EFFECTS ##
################################################################

## HETEROGENEITY BY POVERTY EXPERIMENT

coef <- c(-.03208476, -.05318359, -.0210988, .03006436, -.03172828, -.0617926) 
pk <- data.frame()

lower0.ci.lower <- -.03208476 - (qnorm(.95) * .02840432)
lower0.ci.upper <- -.03208476 + (qnorm(.95) * .02840432) 

lower1.ci.lower <- -.05318359 - (qnorm(.95) * .03318282)
lower1.ci.upper <- -.05318359 + (qnorm(.95) * .03318282)

lower.diff.ci.lower <- -.0210988 - (qnorm(.95) * .0436796)
lower.diff.ci.upper <- -.0210988 + (qnorm(.95) * .0436796)

middle0.ci.lower <- .03006436 - (qnorm(.95) * .02121071)
middle0.ci.upper <- .03006436 + (qnorm(.95) * .02121071)

middle1.ci.lower <-  -.03172828 - (qnorm(.95) * .02081466)
middle1.ci.upper <-  -.03172828 + (qnorm(.95) * .02081466)

middle.diff.ci.lower <- -.0617926 - (qnorm(.95) * .0297177)
middle.diff.ci.upper <- -.0617926 + (qnorm(.95) * .0297177)

figure5a <- ggplot(data = pk, aes(x = c("Lower Class \n Relatively Wealthy", "Lower Class \n Relatively Poor", "Lower Class \n Difference",
                                       "Middle & Upper Class \n Relatively Wealthy", "Middle & Upper Class \n Relatively Poor", "Middle & Upper Class \n Difference"),
                    y = coef)) +
  coord_cartesian(ylim = c(-.23, .15)) +
  geom_point(size = 2.5, color = c("black", "black", "#666666", "black", "black", "#666666")) +
  geom_hline(aes(yintercept=0), colour="red", linetype="dashed") +
  geom_vline(aes(xintercept=3.5), size=.25, color="black") +
  ylab("Endorsement Effect") +
  #ggtitle("(a) Poverty Experiment by SES") +
  theme(axis.title.x = element_blank(), axis.title.y = element_text(size=10),
        axis.text.x=element_text(angle=35, hjust=1, size = 8),
        axis.text.y=element_text(size = 8),
        plot.title = element_text(size = 10),
        panel.grid.major.y = element_line(size=.3),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank()) +
  scale_x_discrete(limits=c("Lower Class \n Relatively Wealthy", "Lower Class \n Relatively Poor", "Lower Class \n Difference",
                                       "Middle & Upper Class \n Relatively Wealthy", "Middle & Upper Class \n Relatively Poor", "Middle & Upper Class \n Difference")) +
  geom_errorbar(aes(ymin=c(lower0.ci.lower, lower1.ci.lower, lower.diff.ci.lower, middle0.ci.lower,
                      middle1.ci.lower, middle.diff.ci.lower),
                    ymax=c(lower0.ci.upper, lower1.ci.upper, lower.diff.ci.upper, middle0.ci.upper,
                      middle1.ci.upper, middle.diff.ci.upper),
                    width=.2), color = c("black", "black", "#666666", "black", "black", "#666666")) +
annotate("text", label = "(a) Poverty Experiment by SES", x = 1.45, y = .125,
         size = 3.25) +
#ggsave(filename = "~/Dropbox/PK2 share/Replication/figure5a.pdf", width = 6.5, height = 4)

## HETEROGENEITY BY NATIONAL VIOLENCE EXPERIMENT

coef2 <- c(.01547399, -.12915575, -.1446297, .02854317, -.06974924, -.0982924) 
pk2 <- data.frame()

lower0.ci.lower2 <- .01547399 - (qnorm(.95) * .04466692)
lower0.ci.upper2 <- .01547399 + (qnorm(.95) * .04466692)

lower1.ci.lower2 <- -.12915575 - (qnorm(.95) * .04275973)
lower1.ci.upper2 <- -.12915575 + (qnorm(.95) * .04275973)

lower.diff.ci.lower2 <- -.1446297 - (qnorm(.95) * .0618347)
lower.diff.ci.upper2 <- -.1446297 + (qnorm(.95) * .0618347)

middle0.ci.lower2 <- .02854317 - (qnorm(.95) * .02993584)
middle0.ci.upper2 <- .02854317 + (qnorm(.95) * .02993584)

middle1.ci.lower2 <-  -.06974924 - (qnorm(.95) * .02633774)
middle1.ci.upper2 <-  -.06974924 + (qnorm(.95) * .02633774)

middle.diff.ci.lower2 <- -.0982924 - (qnorm(.95) * .0398727)
middle.diff.ci.upper2 <- -.0982924 + (qnorm(.95) * .0398727)

figure5b <- ggplot(data = pk2, aes(x = c("Lower Class \n Less Violence", "Lower Class \n More Violence", "Lower Class \n Difference",
                                       "Middle & Upper Class \n Less Violence", "Middle & Upper Class \n More Violence", "Middle & Upper Class \n Difference"),
                    y = coef2)) +
  coord_cartesian(ylim = c(-.275, .15)) +
  geom_point(size = 2.5, color = c("black", "black", "#666666", "black", "black", "#666666")) +
  geom_hline(aes(yintercept=0), colour="red", linetype="dashed") +
  geom_vline(aes(xintercept=3.5), size=.25, color="black") +
  ylab("Endorsement Effect") +
  #ggtitle("(b) Violence Experiment by SES") +
  theme(axis.title.x = element_blank(), axis.title.y = element_text(size=10),
        axis.text.x=element_text(angle=35, hjust=1, size = 8),
        axis.text.y=element_text(size = 8),
        plot.title = element_text(size = 10),
        panel.grid.major.y = element_line(size=.3),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank()) +
  scale_x_discrete(limits=c("Lower Class \n Less Violence", "Lower Class \n More Violence", "Lower Class \n Difference",
                                       "Middle & Upper Class \n Less Violence", "Middle & Upper Class \n More Violence", "Middle & Upper Class \n Difference")) +
  geom_errorbar(aes(ymin=c(lower0.ci.lower2, lower1.ci.lower2, lower.diff.ci.lower2, middle0.ci.lower2,
                      middle1.ci.lower2, middle.diff.ci.lower2),
                    ymax=c(lower0.ci.upper2, lower1.ci.upper2, lower.diff.ci.upper2, middle0.ci.upper2,
                      middle1.ci.upper2, middle.diff.ci.upper2),
                    width=.2), color = c("black", "black", "#666666", "black", "black", "#666666")) +
annotate("text", label = "(b) Violence Experiment by SES", x = 1.45, y = .125,
         size = 3.25) +
#ggsave(filename = "figure5b.pdf", width = 6.5, height = 4)

pdf(file = "~/Dropbox/PK2 share/Replication/Figures/figure5.pdf", height = 8, width = 6.5)
figure5 <- grid.arrange(figure5a, figure5b, ncol=1)
dev.off()

######################################################
################## APPENDIX FIGURES ##################
######################################################

### OVERVIEW OF APPENDIX FIGURES ###  

# Appendix Figure 1: Balance of the Endorsement Experiment
# Appendix Figure 2: Balance of the Poverty Experiment
# Appendix Figure 3: Balance of the Violence Experiment
# Appendix Figure 4: Distribution of Scores on the Knowledge Quiz
# Appendix Figure 5: Distribution of Scores on the Policy Knowledge Index

######################################################
## APPENDIX FIGURE 1: ENDORSEMENT EXP BALANCE PLOT ##
######################################################

#recode 'Status'
ap$stat<-as.character(ap$Status) #change variable type

#create labeller for facets
a_labeller <- function(var, value){ #creats facet labels for the subsequent graph
  value <- as.character(value)
  if (var=="V") { 
    value[value=="0"] <- "Endorsement Experiment "
    value[value=="1"] <- "Poverty Experiment"
    value[value=="2"] <- "Violence Experiment"
  }
  return(value)
}

ape<-subset(ap, V == 0)
endorsebalance <- ggplot(ape) +
  geom_pointrange(aes(ymin=ape$L95, ymax=ape$H95, y=ape$Mean, x=ape$Variable, group=ape$stat, shape=ape$stat), size=0.4,  position=position_dodge(width=0.5)) +
  scale_shape_manual(name = "", labels = c("Treatment", "Control"), breaks=c("1","0"), values = c(3, 1)) +
  scale_x_discrete(name="", limits=c("Asset Index", "Household Expenditures", "Education",
                                     "Math", "Read", "Age", "Household Head", "Gender")) +
  ylab("Mean") +
  # ggtitle("Balance of the Endorsement Experiment") +
  theme(legend.title=element_text(size=12, face="bold", hjust=-0),
        legend.text=element_text(size=10),
        axis.text.x=element_text(colour="#000000", size=10),
        axis.title.x=element_text(hjust=0.48, vjust=0, size=12),
        axis.text.y=element_text(colour="#000000", size=10, hjust=1),
        plot.title = element_text(size=12, face="bold", hjust=0.5, vjust=1)) +
  coord_flip()
ggsave(filename = "~/Dropbox/PK2 share/Replication/Figures/endorsebalance.pdf", width = 6.5, height = 4)


##################################################
## APPENDIX FIGURE 2: POVERTY EXP BALANCE PLOT ##
##################################################

app<-subset(ap, V == 1)
povbalance <- ggplot(app) +
  geom_pointrange(aes(ymin=app$L95, ymax=app$H95, y=app$Mean, x=app$Variable, group=app$stat, shape=app$stat), size=0.4,  position=position_dodge(width=0.5)) +
  scale_shape_manual(name = "", labels = c("Treatment", "Control"), breaks=c("1","0"), values = c(3, 1)) +
  scale_x_discrete(name="", limits=c("Asset Index", "Household Expenditures", "Education",
                                     "Math", "Read", "Age", "Household Head", "Gender")) +
  ylab("Mean") +
  # ggtitle("Balance of the Poverty Experiment") +
  theme(legend.title=element_text(size=12, face="bold", hjust=-0),
        legend.text=element_text(size=10),
        axis.text.x=element_text(colour="#000000", size=10),
        axis.title.x=element_text(hjust=0.48, vjust=0, size=12),
        axis.text.y=element_text(colour="#000000", size=10, hjust=1),
        plot.title = element_text(size=12, face="bold", hjust=0.5, vjust=1)) +
  coord_flip()
ggsave(filename = "~/Dropbox/PK2 share/Replication/Figures/povbalance.pdf", width = 6.5, height = 4)


###################################################
## APPENDIX FIGURE 3: VIOLENCE EXP BALANCE PLOT ##
###################################################

apv<-subset(ap, V == 2)
violbalance <- ggplot(apv) +
  geom_pointrange(aes(ymin=apv$L95, ymax=apv$H95, y=apv$Mean, x=apv$Variable, group=apv$stat, shape=apv$stat), size=0.4, position=position_dodge(width=0.5)) +
  scale_shape_manual(name = "", labels = c("Treatment", "Control"), breaks=c("1","0"), values = c(3, 1)) +
  scale_x_discrete(name="", limits=c("Asset Index", "Household Expenditures", "Education",
                                     "Math", "Read", "Age", "Household Head", "Gender")) +
  ylab("Mean") +
  # ggtitle("Balance of the Violence Experiment") +
  theme(legend.title=element_text(size=12, face="bold", hjust=-0),
        legend.text=element_text(size=10),
        axis.text.x=element_text(colour="#000000", size=10),
        axis.title.x=element_text(hjust=0.48, vjust=0, size=12),
        axis.text.y=element_text(colour="#000000", size=10, hjust=1),
        plot.title = element_text(size=12, face="bold", hjust=0.5, vjust=1)) +
  coord_flip()
ggsave(filename = "~/Dropbox/PK2 share/Replication/Figures/violbalance.pdf", width = 6.5, height = 4)


#####################################################
## APPENDIX FIGURE 4: KNOWLEDGE QUIZ DISTRIBUTION ##
#####################################################

knowquiz <- ggplot(data = pkdata, aes(x = knowledge)) +
  geom_histogram() +
  xlab("Knowledge Quiz") +
  ylab("Density") +
  theme_bw() +    
  theme(axis.line = element_line(size = .35),
        axis.ticks = element_line(size = .35),
        axis.title.x = element_text(size=9), axis.title.y = element_text(size=9),
        axis.text.x=element_text(size = 8),
        axis.text.y=element_text(size = 8),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) +         
  ggsave(filename = "~/Dropbox/PK2 share/Replication/Figures/knowquiz.pdf", width = 4.5, height = 3.5)


####################################################
## APPENDIX FIGURE 5: POLICY KNOWLEDGE HISTOGRAM ##
####################################################

yaxis <- c(23.83, 15.56, 21.3, 17.04, 22.27)
xaxis <- c("0", "1", "2", "3", "4")

data <- data.frame()  

knowpolicy <- ggplot(data = data, aes(x = xaxis, y = yaxis)) +
  coord_cartesian(ylim = c(0, 35)) +
  geom_bar(width=.9, stat="identity") +
  xlab("Knowledge of Policies in Endorsement Experiment") +
  ylab("Percentage") +
  theme_bw() +    
  theme(axis.line = element_line(size = .35),
        axis.ticks = element_line(size = .35),
        axis.title.x = element_text(size=9), axis.title.y = element_text(size=9),
        axis.text.x=element_text(size = 8),
        axis.text.y=element_text(size = 8),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank()) +
  scale_x_discrete(limits=c("0", "1", "2", "3", "4")) +
  ggsave(filename = "~/Dropbox/PK2 share/Replication/Figures/knowpolicy.pdf", width = 4, height = 3.5)





